//
// Created on 2024/4/11.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#include "anisotropic.h"
#include <linux/pcitest.h>
#include <native_drawing/drawing_color.h>
#include <native_drawing/drawing_brush.h>
#include <native_drawing/drawing_image.h>
#include <native_drawing/drawing_matrix.h>
#include <native_drawing/drawing_path.h>
#include <native_drawing/drawing_pen.h>
#include <native_drawing/drawing_round_rect.h>
#include <native_drawing/drawing_round_rect.h>
#include <native_drawing/drawing_sampling_options.h>
#include <native_drawing/drawing_shader_effect.h>
#include <native_drawing/drawing_point.h>
#include <native_drawing/drawing_surface.h>
#include "test_common.h"
#include "common/log_common.h"

enum SkAlphaType : int {
    kUnknown_SkAlphaType,                          //!< uninitialized
    kOpaque_SkAlphaType,                           //!< pixel is opaque
    kPremul_SkAlphaType,                           //!< pixel components are premultiplied by alpha
    kUnpremul_SkAlphaType,                         //!< pixel components are independent of alpha
    kLastEnum_SkAlphaType = kUnpremul_SkAlphaType, //!< last valid value
};


void Anisotropic::OnTestFunction(OH_Drawing_Canvas *canvas) {

    OH_Drawing_Bitmap* bitmap = OH_Drawing_BitmapCreate();
    OH_Drawing_Image* image = OH_Drawing_ImageCreate();
    OH_Drawing_BitmapFormat cFormat{COLOR_FORMAT_BGRA_8888, ALPHA_FORMAT_OPAQUE};
    OH_Drawing_BitmapBuild(bitmap, kImageSize, kImageSize, &cFormat);
    OH_Drawing_Canvas* bimap_canvas = OH_Drawing_CanvasCreate();
    OH_Drawing_CanvasBind(bimap_canvas, bitmap);
    OH_Drawing_ImageBuildFromBitmap(image, bitmap);
    
    OH_Drawing_CanvasClear(bimap_canvas, 0xFFFFFFFF);
    
    OH_Drawing_Pen* pen = OH_Drawing_PenCreate();
    OH_Drawing_PenSetAntiAlias(pen, true);
    OH_Drawing_CanvasAttachPen(bimap_canvas, pen);
    
    float angle = 0.0f, sin, cos;
    OH_Drawing_CanvasTranslate(bimap_canvas, kImageSize / 2.0f, kImageSize / 2.0f);
    for (int i = 0; i < kNumLines; ++i, angle += kAngleStep) {
        sin = sinf(angle);
        cos = cosf(angle);
        OH_Drawing_CanvasDrawLine(bimap_canvas, cos * kInnerOffset, sin * kInnerOffset,
        cos * kImageSize/2, sin * kImageSize/2);
    }
    
    
    OH_Drawing_SamplingOptions *fsampling = OH_Drawing_SamplingOptionsCreate(OH_Drawing_FilterMode::FILTER_MODE_NEAREST, OH_Drawing_MipmapMode::MIPMAP_MODE_LINEAR);

    float gScales[] = {0.9f, 0.8f, 0.75f, 0.6f, 0.5f, 0.4f, 0.25f, 0.2f, 0.1f};

    for (int i = 0; i < sizeof(gScales); ++i) {
        int height = (int)(OH_Drawing_BitmapGetHeight(bitmap) * gScales[i]);
        
        int yOff;
        if (i <= (int)sizeof(gScales) / 2) {
            yOff = kSpacer + i * (OH_Drawing_BitmapGetHeight(bitmap) + kSpacer);
        } else {
            yOff = (sizeof(gScales) - i) * (OH_Drawing_BitmapGetHeight(bitmap) + kSpacer) - height;
        }
        
        OH_Drawing_Rect* rect = OH_Drawing_RectCreate((float)kSpacer, (float)yOff, (float)OH_Drawing_BitmapGetWidth(bitmap), (float)height);
        OH_Drawing_CanvasClear(canvas, 0xFFCCCCCC);
        OH_Drawing_CanvasDrawImageRect(canvas, image, rect, fsampling);
        
        OH_Drawing_CanvasDrawRect(canvas, rect);
    }

    for (int i = 0; i < sizeof(gScales); ++i) {
        int width = (int)(OH_Drawing_BitmapGetWidth(bitmap) * gScales[i]);
        
        int xOff, yOff;
        if (i <= (int)sizeof(gScales) / 2) {
            xOff = OH_Drawing_BitmapGetWidth(bitmap) + 2 * kSpacer;
            yOff = kSpacer + i * (OH_Drawing_BitmapGetHeight(bitmap) + kSpacer);
        } else {
            xOff = OH_Drawing_BitmapGetWidth(bitmap) + 2 * kSpacer + OH_Drawing_BitmapGetWidth(bitmap) - width;
            yOff = kSpacer + (sizeof(gScales) - i - 1) * (OH_Drawing_BitmapGetHeight(bitmap) + kSpacer);
        }
        
        OH_Drawing_Rect* rect = OH_Drawing_RectCreate((float)kSpacer, (float)yOff, (float)width, (float)OH_Drawing_BitmapGetHeight(bitmap));
        OH_Drawing_CanvasClear(canvas, 0xFFCCCCCC);
        OH_Drawing_CanvasDrawImageRect(canvas, image, rect, fsampling);
        OH_Drawing_CanvasDrawRect(canvas, rect);
        
    }
    OH_Drawing_BitmapDestroy(bitmap);
}
